Submission¶

Put the ipynb file and html file in the github branch you created in the last assignment and submit the link to the commit in brightspace

In [9]:
from plotly.offline import init_notebook_mode
import plotly.io as pio
import plotly.express as px

init_notebook_mode(connected=True)
pio.renderers.default = "plotly_mimetype+notebook"
In [10]:
#load data
df = px.data.gapminder()
df.head()
Out[10]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4
1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4
2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4
3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4
4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4

Question 1:¶

Recreate the barplot below that shows the population of different continents for the year 2007.

Hints:

  • Extract the 2007 year data from the dataframe. You have to process the data accordingly
  • use plotly bar
  • Add different colors for different continents
  • Sort the order of the continent for the visualisation. Use axis layout setting
  • Add text to each bar that represents the population
In [11]:
# YOUR CODE HERE

df_2007 = df[df['year'] == 2007]

df_grouped = df_2007.groupby('continent', as_index=False)['pop'].sum()


fig = px.bar(df_grouped, 
             x='pop', 
             y='continent', 
             color='continent', 
             title="Population of Continents in 2007")

fig.update_layout(xaxis_title="Population", 
                  yaxis_title="Continent", 
                  showlegend=False)

fig.show()

Question 2:¶

Sort the order of the continent for the visualisation

Hint: Use axis layout setting

In [12]:
# YOUR CODE HERE
df_2007 = df[df['year'] == 2007]

df_grouped = df_2007.groupby('continent', as_index=False)['pop'].sum()
df_grouped = df_grouped.sort_values(by='pop', ascending=False)

fig = px.bar(df_grouped, 
             x='pop', 
             y='continent', 
             color='continent',  
             title="Population of Continents in 2007")

fig.update_layout(xaxis_title="Population", 
                  yaxis_title="Continent", 
                  showlegend=False)

fig.show()

Question 3:¶

Add text to each bar that represents the population

In [13]:
# YOUR CODE HERE
df_2007 = df[df['year'] == 2007]

df_grouped = df_2007.groupby('continent', as_index=False)['pop'].sum()
df_grouped = df_grouped.sort_values(by='pop', ascending=False)
df_grouped['pop_G'] = df_grouped['pop'] / 1e9
df_grouped['pop_G'] = df_grouped['pop_G'].apply(lambda x: f'{x:.1f}G' if x >= 1 else f'{x*1000:.0f}M')

fig = px.bar(df_grouped, 
             x='pop', 
             y='continent', 
             color='continent', 
             text='pop_G', 
             title="Population of Continents in 2007")

fig.update_layout(xaxis_title="Population", 
                  yaxis_title="Continent", 
                  showlegend=False)

fig.show()

Question 4:¶

Thus far we looked at data from one year (2007). Lets create an animation to see the population growth of the continents through the years

In [23]:
# YOUR CODE HERE
df_grouped = df.groupby(['continent', 'year'], as_index=False)['pop'].sum()

df_grouped = df_grouped.sort_values(by=['year', 'pop'], ascending=[True, False])

df_grouped['continent'] = df_grouped['continent'].astype('category')

fig = px.bar(df_grouped, 
             x='pop', 
             y='continent', 
             color='continent', 
             animation_frame='year', 
             animation_group='continent',
             range_x=[0, 4e9],  
             title="Population of Continents from 1952 to 2007")

fig.update_layout(xaxis_title="Population", 
                  yaxis_title="Continent", 
                  yaxis={'categoryorder':'total ascending'},
                  showlegend=False)

fig['layout']['updatemenus'][0]['buttons'][0]['args'][1]['frame']['redraw'] = True

fig.show()

Question 5:¶

Instead of the continents, lets look at individual countries. Create an animation that shows the population growth of the countries through the years

In [30]:
# YOUR CODE HERE
df_sorted = df.sort_values(by=['year', 'pop'], ascending=[True, False])

fig = px.bar(df_sorted, 
             x='pop', 
             y='country', 
             color='country', 
             animation_frame='year', 
             animation_group='country',
             range_x=[0, 1.5e9],  
             title="Population Growth of Countries from 1952 to 2007")

fig.update_layout(xaxis_title="Population", 
                  yaxis_title="Country", 
                  yaxis={'categoryorder':'total ascending'},  
                  showlegend=False)

fig['layout']['updatemenus'][0]['buttons'][0]['args'][1]['frame']['redraw'] = True

fig.show()

Question 6:¶

Clean up the country animation. Set the height size of the figure to 1000 to have a better view of the animation

In [31]:
# YOUR CODE HERE
df_sorted = df.sort_values(by=['year', 'pop'], ascending=[True, False])

fig = px.bar(df_sorted, 
             x='pop', 
             y='country', 
             color='country', 
             animation_frame='year', 
             animation_group='country',
             range_x=[0, 1.5e9],  
             title="Population Growth of Countries from 1952 to 2007")

fig.update_layout(xaxis_title="Population", 
                  yaxis_title="Country", 
                  yaxis={'categoryorder':'total ascending'},
                  showlegend=False,
                  height=1000) 

fig['layout']['updatemenus'][0]['buttons'][0]['args'][1]['frame']['redraw'] = True

fig.show()

Question 7:¶

Show only the top 10 countries in the animation

Hint: Use the axis limit to set this.

In [33]:
# YOUR CODE HERE
df_sorted = df.sort_values(by=['year', 'pop'], ascending=[True, False])

df_top10 = df_sorted.groupby('year').head(10)

fig = px.bar(df_top10, 
             x='pop', 
             y='country', 
             color='country', 
             animation_frame='year', 
             animation_group='country',
             range_x=[0, 1.5e9],  
             title="Top 10 Population Growth of Countries from 1952 to 2007")


fig.update_layout(xaxis_title="Population", 
                  yaxis_title="Country", 
                  yaxis={'categoryorder':'total ascending'}, 
                  showlegend=False,
                  height=1000)

fig['layout']['updatemenus'][0]['buttons'][0]['args'][1]['frame']['redraw'] = True

fig.show()
In [ ]: